{{!@formatter:off}}
// @formatter:off
package {{generatedPackage}};

import javax.annotation.Generated;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.jetbrains.annotations.UnmodifiableView;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;

import cn.javaer.snippets.jooq.TableMeta;
import cn.javaer.snippets.jooq.ColumnMeta;
import {{entityClassName}};

/**
 * This class is automatic generated.
 */
@SuppressWarnings({"ALL"})
{{#if hasGeneratedAnnotation}}
@Generated("cn.javaer.snippets.jooq.codegen.withentity.CodeGenTool")
{{~/if}}
public class {{tableClassName}} extends TableImpl<Record> implements TableMeta<{{entityName}}, {{#if idColumnMeta}}{{idColumnMeta.fieldType}}{{else}}Void{{/if}}, {{#if hasAuditor}}{{auditorType}}{{else}}Void{{/if}}> {

    public static final {{tableClassName}} {{staticFieldName}} = new {{tableClassName}}();

    public static final Field<?>[] {{staticFieldName}}_FIELDS = new Field[]{ {{#each selectColumnMetas}}{{staticFieldName}}.{{tableFieldName}}{{#unless @last}},{{/unless}}{{/each}} };
{{#each allColumnMetas}}
{{#if customField}}
    public final {{customFieldType}}<Record, {{fieldType}}> {{tableFieldName}} = new {{customFieldType}}("{{columnName}}", {{sqlType}}, this);
{{~else}}
    public final TableField<Record, {{fieldType}}> {{tableFieldName}} = createField(DSL.name("{{columnName}}"), {{sqlType}}, this, ""{{#if converter}}, {{{converter}}}{{/if}});
{{~/if~}}
{{/each}}

    private final Table<?> _table = DSL.table(getUnqualifiedName());

    private final List<Field<?>> _selectFields = Arrays.asList({{#each selectColumnMetas}}this.{{tableFieldName}}{{#unless @last}},{{/unless}}{{/each}});

    private final List<ColumnMeta<{{entityName}}, ?>> _savedColumnMetas = Arrays.asList({{#each savedColumnMetas}}new ColumnMeta((Function<{{entityName}}, {{fieldType}}>) {{entityName}}::{{getterName}}, this.{{tableFieldName}}){{#unless @last}},{{/unless}}{{/each}});
{{#if idColumnMeta}}
    private final ColumnMeta<{{entityName}}, {{idColumnMeta.fieldType}}> _idMeta = new ColumnMeta<>({{entityName}}::{{idColumnMeta.getterName}}, this.{{idColumnMeta.tableFieldName}});
{{/if~}}
{{#if updatedByColumnMeta}}
    private final ColumnMeta<{{entityName}}, {{updatedByColumnMeta.fieldType}}> _updatedByMeta = new ColumnMeta<>({{entityName}}::{{updatedByColumnMeta.getterName}}, this.{{updatedByColumnMeta.tableFieldName}});
{{/if~}}
{{#if updatedDateColumnMeta}}
    private final ColumnMeta<{{entityName}}, {{updatedDateColumnMeta.fieldType}}> _updatedDateMeta = new ColumnMeta<>({{entityName}}::{{updatedDateColumnMeta.getterName}}, this.{{updatedDateColumnMeta.tableFieldName}});
{{/if~}}
{{#if createdByColumnMeta}}
    private final ColumnMeta<{{entityName}}, {{createdByColumnMeta.fieldType}}> _createdByMeta = new ColumnMeta<>({{entityName}}::{{createdByColumnMeta.getterName}}, this.{{createdByColumnMeta.tableFieldName}});
{{/if~}}
{{#if createdDateColumnMeta}}
    private final ColumnMeta<{{entityName}}, {{createdDateColumnMeta.fieldType}}> _createdDateMeta = new ColumnMeta<>({{entityName}}::{{createdDateColumnMeta.getterName}}, this.{{createdDateColumnMeta.tableFieldName}});
{{/if}}
    public {{tableClassName}}() {
        this(DSL.name("{{tableName}}"), null);
    }

    private {{tableClassName}}(Name alias, Table<Record> aliased) {
        this(alias, aliased, null);
    }

    private {{tableClassName}}(Name alias, Table<Record> aliased, Field<?>[] parameters) {
        super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
    }

    @Override
    public {{tableClassName}} as(String alias) {
        return new {{tableClassName}}(DSL.name(alias), this);
    }

    @Override
    public Table<?> getTable() {
        return this._table;
    }

    @Override
    public Class getEntityClass() {
        return {{entityName}}.class;
    }
{{#if idColumnMeta~}}
{{#if idColumnMeta.readOnly}}{{else}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{idColumnMeta.fieldType}}>> idGenerator() {
        return Optional.of(this._idMeta);
    }
{{/if}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{idColumnMeta.fieldType}}>> getId() {
        return Optional.of(this._idMeta);
    }
{{/if~}}
{{#if updatedByColumnMeta}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{updatedByColumnMeta.fieldType}}>> getUpdatedBy() {
        return Optional.of(this._updatedByMeta);
    }
{{/if~}}
{{#if updatedDateColumnMeta}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{updatedDateColumnMeta.fieldType}}>> getUpdatedDate() {
        return Optional.of(this._updatedDateMeta);
    }
{{/if~}}
{{#if createdByColumnMeta}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{createdByColumnMeta.fieldType}}>> getCreatedBy() {
        return Optional.of(this._createdByMeta);
    }
{{/if}}
{{#if createdDateColumnMeta}}
    @Override
    public Optional<ColumnMeta<{{entityName}}, {{createdDateColumnMeta.fieldType}}>> getCreatedDate() {
        return Optional.of(this._createdDateMeta);
    }
{{/if}}
    @Override
    public @UnmodifiableView List<Field<?>> selectFields() {
        return _selectFields;
    }

    @Override
    public @UnmodifiableView List<ColumnMeta<{{entityName}}, ?>> saveColumnMetas() {
        return this._savedColumnMetas;
    }
}